//const { composeRawBufferEntity3DWhole } = require("XrFrame/kanata/lib/index")
const app = getApp()
// pages/me/myOrders/myOrders.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
  status:2
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad:function(options) {
    this.setData({
      status:options.status
   })
    this.getOrderList()
    console.log(app.globalData.openid)
   
  },
  //   wx.cloud.database().collection('shop_orders')
  //   .where({
  //     status:-1
  //   })
  //   .get()
  //   .then(res=>{
  //     console.log(res)
  //     this .setData({
  //       orderList:res.data
  //     })
  //   })
  // })
// console.log(options.status)

// this.getOrderList()
// console.log(app.globalData.openid)
//   },
  onUnload(){
    // this.data.watcher.close()
  },
  chooseType(event){
    console.log(event.currentTarget.dataset.type)
    let status = event.currentTarget.dataset.type
    this.setData({
      status
    })
    this.getOrderList()
    wx.cloud.database().collection('shop_orders')
    .where({
      status:Number(this.data.status),
       _openid:app.globalData.openid 
    })
    .orderBy('time','desc')
    .get()
    .then(res  =>{
      console.log(res)
      this.setData({
        orderList:res.data
      })
    })
      },
      getOrderList(){
        wx.cloud.database().collection('shop_orders')
        .where({
          status:Number(this.data.status),
          _openid:app.globalData.openid
        })
        .orderBy('time','desc')
        .get()
        .then(res=>{
          console.log(res)
          this.setData({
            orderList:res.data
          })
        })
      },

//去支付
     pay(event){
     let index = event.currentTarget.dataset.index
     console.log(index)
     let that = this;
    // this.setData({
    //   status:options.status
    // })
    wx.showModal({
      title:'提示',
      content:'是否支付商品价格'+this.data.orderList[index].totalMoney+'元',
      confirmText:'支付'
    })
    .then(res=>{
      console.log(res)
      if(res.confirm == true){
        wx.cloud.database().collection('shop_orders').doc(this.data.orderList[index]._id)
        .update({
          data:{
            status:1
          }
        })
        .then(result=>{
        console.log(result)
        //从购物车清除订单里边的商品
        that.clearCarList()
        //wx.navigateBack({
         // delta:0,
        //   success(){
        //     wx.request({
        //       url: '127.0.0.1',
        //     })
        //
            wx.showToast({
              title: '支付成功',
            })
            this.getOrderList()

         // })
        })
        wx.request({
          url: 'http://47.115.214.192', //仅为示例，并非真实的接口地址
          method:'POST',
          data: {
            msg: '重量',
            token: '10'
          },
          header: {
            'content-type': 'application/json' // 默认值
          },
          success (res) {
            console.log(res.data)
          }
        })
        }else{
        wx.showToast({
          icon:'error',
          title: '支付失败',
        })
      }
            })
        },


//取消订单
cancel(event){
  let index = event.currentTarget.dataset.index
  console.log(index)
  wx.showModal({
    title: '提示',
    content: '是否取消订单',
    confirmText:'确定'
        
 })
  .then(res=>{
    if(res.confirm ==true){
      wx.cloud.database().collection('shop_orders').doc(this.data.orderList[index]._id)
      .update({
        data:{
          status:-2
        }
      })
      .then(result=>{
        console.log(result)
        wx.showToast({
          title: '取消成功',
        })
        //退款
        //this.refund()
        this.getOrderList()
      })
    }else{

    }
  })
    },
    tuikuan(event){
      let index = event.currentTarget.dataset.index
      console.log(index)
      wx.showModal({
        title: '提示',
        content: '是否申请退款',
        confirmText:'确定'
            
     })
      .then(res=>{
        if(res.confirm ==true){
          wx.cloud.database().collection('shop_orders').doc(this.data.orderList[index]._id)
          .update({
            data:{
              status:-3
            }
          })
          .then(result=>{
            console.log(result)
            wx.showToast({
              title: '退款成功',
            })
            //退款
            //this.refund()
            this.getOrderList()
          })
        }else{

        }
      })
        },
        confirm(event){
          let index = event.currentTarget.dataset.index
          console.log(index)
        wx.showModal({
          title: '提示',
          content: '确认已拿到产品吗',
          confirmText:'确定'
              
       })
        .then(res=>{
          if(res.confirm ==true){
            wx.cloud.database().collection('shop_orders').doc(this.data.orderList[index]._id)
            .update({
              data:{
                status:2
              }
            })
            .then(result=>{
              console.log(result)
              wx.showToast({
                title: '保存成功',
              }) 
              this.getOrderList()
            })
      }
    
    }) 
  },
//支付后清空购物车
  clearCarList(){
    for (let  i in app.globalData.cartList){
      for(let j in app.globalData.orderList){
        if(app.globalData.orderList[j]._id == app.globalData.cartList[i]._id){
        app.globalData.cartList.splice(i,1)
        }
      }
    }
    wx.setStorageSync('cartList', app.globalData.cartList)
  },
})
